package smart.src;

import java.awt.Color;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.openswing.swing.message.receive.java.ErrorResponse;
import org.openswing.swing.message.receive.java.Response;
import org.openswing.swing.message.receive.java.VOListResponse;
import org.openswing.swing.table.client.GridController;
import org.openswing.swing.table.java.GridDataLocator;

import src.DAO.CardDaoImpl;
import src.VO.CardVO;

import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.support.ConnectionSource;


public class CardGridFrameController extends GridController implements
		GridDataLocator {

	CardDaoImpl cardDAO = null;
	CardGridFrame grid = null;
	ConnectionSource connectionSource;

	public CardGridFrameController(ConnectionSource connectionSource) {
		this.connectionSource = connectionSource;
		grid = new CardGridFrame(this);
	}

	@Override
	public Response loadData(int action, int startIndex, Map filteredColumns,
			ArrayList currentSortedColumns,
			ArrayList currentSortedVersusColumns, Class valueObjectType,
			Map otherGridParams) {

		try {
			cardDAO = new CardDaoImpl(connectionSource);
			List<CardVO> result;
			QueryBuilder<CardVO, Integer> cardQb = cardDAO.queryBuilder();

			cardQb.where().gt("idcards", 1);
			PreparedQuery<CardVO> querry = cardQb.prepare();
			result = cardDAO.query(querry);
			return new VOListResponse(result, false, result.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ErrorResponse(e.getMessage());
		}

		
	}

	@Override
	public Color getBackgroundColor(int row, String attributeName, Object value) {
		   
		if(attributeName.equals("status"))
		{
			String status = (String)value;
			if (status.equals("GOLD"))
		          return Color.YELLOW;
			else if (status.equals("SILVER"))
				return Color.GRAY;
			else if (status.equals("PREMIUM"))
				return Color.MAGENTA;
		}
		return super.getBackgroundColor(row, attributeName, value);
	}
}
